using Godot;
using System;

namespace Darksky.GodotAddons.GizmosDisplay;


[Tool]
public partial class RectGizmos : Node2D
{
	[Export]
	public Rect2I Rect { get => _rect; set { UpdateRect(value); } }
	private Rect2I _rect = new(), _drawRect;

	[Export]
	public Color Color { get => _color; set { _color = value; QueueRedraw(); } }
	private Color _color = new Color("#39C5BB", 0.2f);

	[Export]
	public bool Fill { get => _fill; set { _fill = value; QueueRedraw(); } }
	private bool _fill = true;

	[Export]
	public bool DrawInGame { get; set; } = false;


	private bool _editorHint = false;

	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		_editorHint = Engine.IsEditorHint();
		_drawRect = _rect;
	}

    public override void _Draw()
    {
		if (_editorHint || DrawInGame)
		{
			DrawRect(_drawRect, _color, _fill);
		}
    }

	private void UpdateRect(Rect2I rect)
	{
        _rect = rect;
		_drawRect = _rect;
		_drawRect.Position -= rect.Size / 2;
		QueueRedraw();
	}

}
